package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressBookMapper {

    /**
     * 添加地址列表
     * @param addressBook
     */
    @Insert("insert into address_book(user_id, consignee, sex, phone, province_code, province_name, city_code, city_name, district_code, district_name, detail, label)" +
            "values (#{userId}, #{consignee}, #{sex}, #{phone}, #{provinceCode} , #{provinceName} , #{cityCode} , #{cityName} , #{districtCode} , #{districtName} , #{detail} , #{label})")
    void insertAddress(AddressBook addressBook);

    /**
     * 查询当前用户的地址列表
     * @param addressBook
     * @return
     */
    @Select("select * from address_book where user_id = #{userId}")
    List<AddressBook> selectUserAddress(AddressBook addressBook);

    /**
     * 查看默认地址
     * @param addressBook
     */
    @Select("select * from address_book where user_id = #{userId} and is_default = 1")
    AddressBook selectDefaultAddress(AddressBook addressBook);

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Update("update address_book set is_default = #{isDefault} where user_id = #{userId} and id = #{id}")
    void updateDefaultAddress(AddressBook addressBook);

    /**
     * 将当前用户的默认地址设为0
     * @param addressBook
     */
    @Update("update address_book set is_default = #{isDefault} where user_id = #{userId}")
    void updateDefaultUser(AddressBook addressBook);

    /**
     * 删除选中地址
     * @param addressBook
     */
    @Delete("delete from address_book where user_id = #{userId} and id = #{id}")
    void deleteAddress(AddressBook addressBook);

    /**
     * 根据id查看地址
     * @param addressBook
     * @return
     */
    @Select("select * from address_book where id = #{id}")
    AddressBook selectAddressById(AddressBook addressBook);

    /**
     * 根据id修改地址
     * @param addressBook
     */
    @Update("update address_book set consignee = #{consignee}, sex = #{sex}, phone = #{phone}, province_code = #{provinceCode}, province_name = #{provinceName}," +
            "city_code = #{cityCode}, city_name = #{cityName}, district_code = #{districtCode}, district_name = #{districtName}, detail = #{detail}, label = #{label} where id = #{id} and user_id = #{userId}")
    void updateAddressById(AddressBook addressBook);
}
